export const DateUtil = {
  /**
   * 时间格式化
   * @param date
   * @param format
   * @returns {*}
   */
  format (date, format) {
      if (!date) {
          return null
      }
      format = format || 'yyyy-MM-dd'
      if (date instanceof Date) {
          // 无需转换
      } else {
          switch (typeof date) {
              case 'string':
                  if (date.indexOf('T') > -1) {
                      date = new Date(date)
                  } else if (/^\d+$/.test(date)) {
                      date = new Date(Number(date))
                  } else {
                      date = new Date(date.replace(/-/g, '/'))
                  }
                  break
              case 'number':
                  date = new Date(date)
                  break
              default:
                  date = new Date()
                  break
          }
      }
      if (!(date instanceof Date)) return
      // 计算年的后两位
      let shortYear = date.getYear()
      shortYear = shortYear < 2000 ? shortYear + 1900 : shortYear
      const yy = shortYear.toString().substr(2, 2)
      // 时间格式
      const dict = {
          'yy': yy,
          'yyyy': date.getFullYear(),
          'M': date.getMonth() + 1,
          'd': date.getDate(),
          'H': date.getHours(),
          'm': date.getMinutes(),
          's': date.getSeconds(),
          'MM': ('' + (date.getMonth() + 101)).substr(1),
          'dd': ('' + (date.getDate() + 100)).substr(1),
          'HH': ('' + (date.getHours() + 100)).substr(1),
          'mm': ('' + (date.getMinutes() + 100)).substr(1),
          'ss': ('' + (date.getSeconds() + 100)).substr(1)
      }
      return format.replace(/(y+|MM?|dd?|HH?|ss?|mm?)/g, function () {
          return dict[arguments[0]]
      })
  },
  /**
   * 指定格式获取当前日期
   * @param format
   * @returns {*}
   */
  getCurrentDate (format) {
      return this.format(new Date(), format)
  },
  /**
   * 获取当前日期
   * @returns {*}
   */
  getCurrentDateTime () {
      return this.format(new Date(), 'yyyy-MM-dd HH:mm:ss')
  },
  /**
   * 获取日期中的年月
   * @param date
   * @returns {*}
   */
  getYearMonth (date, month) {
    if (!month) {
      return this.format(date, 'yyyy-MM')
    } else {
      return this.format(date + '-' + month, 'yyyy-MM')
    }
  },
  /**
   * 获取日期中的年份
   * @param date
   * @returns {*}
   */
  getYear (date) {
      if (date) {
          if (!date) {
              const d = new Date()
              return d.getFullYear()
          } else if (date instanceof Date) {
              // 无需转换
          } else {
              switch (typeof date) {
                  case 'string':
                      date = new Date(date.replace(/-/g, '/'))
                      break
                  case 'number':
                      date = new Date(date)
                      break
                  default:
                      break
              }
          }
          if (!(date instanceof Date)) return ''
          return date.getFullYear()
      }
  },
  /**
   * 获取日期中的月
   * @param date
   * @returns {*}
   */
  getMonth (date) {
      if (!date) {
          const d = new Date()
          return d.getMonth() + 1
      } else if (date instanceof Date) {
          // 无需转换
      } else {
          switch (typeof date) {
              case 'string':
                  date = new Date(date.replace(/-/g, '/'))
                  break
              case 'number':
                  date = new Date(date)
                  break
              default:
                  break
          }
      }
      if (!(date instanceof Date)) return ''
      return date.getMonth() + 1
  },
  /**
   * 获取日期中的日
   * @param date
   * @returns {*}
   */
  getDay (date) {
      if (!date) {
          const d = new Date()
          return d.getDate()
      } else if (date instanceof Date) {
          // 无需转换
      } else {
          switch (typeof date) {
              case 'string':
                  date = new Date(date.replace(/-/g, '/'))
                  break
              case 'number':
                  date = new Date(date)
                  break
              default:
                  break
          }
      }
      if (!(date instanceof Date)) return ''
      return date.getDate()
  },
  /**
   * 获取指定日期所在月的天数
   * @param date
   * @returns {*}
   */
  getDaysOfMonth (date) {
      if (!date) {
          const d = new Date()
          return d.getDate()
      } else if (date instanceof Date) {
          // 无需转换
      } else {
          switch (typeof date) {
              case 'string':
                  date = new Date(date.replace(/-/g, '/'))
                  break
              case 'number':
                  date = new Date(date)
                  break
              default:
                  break
          }
      }
      if (!(date instanceof Date)) return ''
      const thisDate = new Date(date.getFullYear(), date.getMonth() + 1, 0) // 当天数为0 js自动处理为上一月的最后一天
      return thisDate.getDate()
  },
  /**
   * 当月第一天
   * @returns {Date}
   */
  getFirstDayOfCurrentMonth () {
      const date = new Date()
      date.setDate(1)
      return date
  },
  /**
   * 当月最后一天
   * @returns {Date}
   */
  getLastDayOfCurrentMonth () {
      const date = new Date()
      let currentMonth = date.getMonth()
      const nextMonth = ++currentMonth
      const nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1)
      const oneDay = 1000 * 60 * 60 * 24
      return new Date(nextMonthFirstDay - oneDay)
  },
  /**
   * 上月第一天
   */
  getFirstDayOfLastMonth () {
      const nowdays = new Date()
      let year = nowdays.getFullYear()
      let month = nowdays.getMonth()
      if (month === 0) {
          month = 12
          year = year - 1
      }
      if (month < 10) {
          month = '0' + month
      }
      const myDate = new Date(year, month, 1)
      return myDate
  },
  /**
   * 上月最后一天
   * @returns {Date}
   */
  getLastDayOfLastMonth () {
      const nowdays = new Date()
      const year = nowdays.getFullYear()
      const month = nowdays.getMonth()
      const myDate = new Date(year, month, 0)
      return myDate
  },
  /**
   * 获取一天的开始时间
   * @returns {Date}
   */
  getBeginOfDay (d) {
    d = d || new Date()
    return this.format(d, 'yyyy-MM-dd 00:00:00')
  },
  /**
   * 获取一天的结束时间
   * @returns {Date}
   */
  getEndOfDay (d) {
    d = d || new Date()
    return this.format(d, 'yyyy-MM-dd 23:59:59')
  },
  /**
   * 获取1个月后的日期
   * @param date
   * @returns {string}
   */
  getNextMonth (date) {
      return this.dateAddMonth(date, 1)
  },
  /**
   * 获取1年后的日期
   * @param date
   * @returns {string}
   */
  getNextYear (date) {
      return this.dateAddMonth(date, 12)
  },
  /**
   * m个月后的日期
   * @param d
   * @param m
   * @returns {string}
   */
  dateAddMonth (d, m) {
      d = this.format(d)
      if (d && d.indexOf('-') > -1) {
          const ds = d.split('-')
          const _d = ds[2] - 0
          const nextM = new Date(ds[0], ds[1] - 1 + m + 1, 0)
          const max = nextM.getDate()
          d = new Date(ds[0], ds[1] - 1 + m, _d > max ? max : _d)
          return d.toLocaleDateString().match(/\d+/g).join('-')
      }
      return null
  },
  /**
   * m天后的日期
   * @param d
   * @param m
   * @returns {string}
   */
  dateAddDay (d, m) {
      if (d instanceof Date) {
          // 无需转换
      } else {
          switch (typeof d) {
              case 'string':
                  d = new Date(d.replace(/-/g, '/'))
                  break
              case 'number':
                  d = new Date(d)
                  break
              default:
                  return null
          }
      }
      if (!(d instanceof Date)) return
      d.setDate(d.getDate() + m)
      return this.format(d)
  },
  /**
   * 比较日期
   * @param startDate
   * @param endDate
   * @returns {number}
   */
  dateCompare (startDate, endDate) {
      const d1 = new Date(startDate.replace(/-/g, '/')).getTime()
      const d2 = new Date(endDate.replace(/-/g, '/')).getTime()
      if (d1 === d2) {
          return 0
      } else if (d1 > d2) {
          return 1
      } else {
          return -1
      }
  },
  /**
   *  计算两个日期间的相差天数
   * @param d1
   * @param d2
   * @returns {number}
   */
  dateDiff (d1, d2) {
      if (!d1 || !d2) { return null }
      const day = 24 * 60 * 60 * 1000
      d1 = this.format(d1)
      d2 = this.format(d2)
      try {
          const dateArr = d1.split('-')
          const checkDate = new Date()
          checkDate.setFullYear(dateArr[0], dateArr[1] - 1, dateArr[2])
          const checkTime = checkDate.getTime()
          const dateArr2 = d2.split('-')
          const checkDate2 = new Date()
          checkDate2.setFullYear(dateArr2[0], dateArr2[1] - 1, dateArr2[2])
          const checkTime2 = checkDate2.getTime()
          const diff = (checkTime - checkTime2) / day
          return Math.ceil(diff)
      } catch (e) {
          return null
      }
  },
  /**
   *  计算两个日期间的月差
   * @param d1
   * @param d2
   * @param precise 精确到天
   * @returns {number}
   */
  monthDiff (d1, d2, precise) {
      if (!d1 || !d2) return null
      precise = precise || true
      const year1 = this.getYear(d1)
      const month1 = this.getMonth(d1)
      const year2 = this.getYear(d2)
      const month2 = this.getMonth(d2)
      let offset = 0
      if (d1 > d2 && precise) {
          const day1 = this.getDay(d1)
          const day2 = this.getDay(d2)
          if (day1 > day2) {
              offset = 1
          }
      }
      return year1 * 12 + month1 - (year2 * 12 + month2) + offset
  },
  /**
   * 获取前几天
   * @param gap
   * @returns {Date}
   */
  beforeDate (gap) {
      const beforeDate = new Date()
      beforeDate.setDate(beforeDate.getDate() - gap)
      return beforeDate
  },
  /**
   * 新增日期范围
   * @param params
   * @param dateRange
   * @param 开始结束日期字段后缀
   * @returns {*}
   */
  addDateRange (params, dateRange, propName) {
    const search = params
    search.params = typeof (search.params) === 'object' && search.params !== null && !Array.isArray(search.params) ? search.params : {}
    dateRange = Array.isArray(dateRange) ? dateRange : []
    if (typeof (propName) === 'undefined') {
      search.params['beginTime'] = dateRange[0]
      search.params['endTime'] = dateRange[1]
    } else {
      search.params['begin' + propName] = dateRange[0]
      search.params['end' + propName] = dateRange[1]
    }
    return search
  },
  /**
   * 获取月份字典
   * @param
   * @returns {*}
   */
  getDictMonth () {
    const array = []
    for (let index = 1; index <= 12; index++) {
      const item = {}
      item.dictLabel = index + '月'
      item.dictValue = '' + index
      array.push(item)
    }
    return array
  },
  /**
   * 获取季度字典
   * @param
   * @returns {*}
   */
  getDictQuarter () {
    const array = []
    for (let index = 1; index <= 4; index++) {
      const item = {}
      let label = ''
      switch (index) {
        case 1:
          label = '一'
          break
        case 2:
          label = '二'
          break
        case 3:
          label = '三'
          break
        case 4:
          label = '四'
          break
      }
      item.dictLabel = label + '季度'
      item.dictValue = '' + index
      array.push(item)
    }
    return array
  },
  /**
   * 获取指定日期是星期几
   * @param {*} value
   * @returns
   */
  getWeek (value) {
    const weekArray = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
    const week = (new Date(value)).getDay()

    return weekArray[week]
  }
}
